#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "DHT11.h"
#include "Serial.h"
#include "BH1750.h"
#include "Timer.h"
#include "Led.h"
#include "Beep.h"
#include "KeyExti.h"

DHT11_Humi_Temp DHT11_Data;

uint8_t Alarm_Flag = 0;
uint8_t Alarm_Free = 0;
uint8_t KeyNumber = 0;


int main(void)
{
	OLED_Init();
	Serial_Init();
	DHT11_Init();
	BH1750_Init();
	Led_Init();
	Beep_Init();
	Timer2_Init();
	Timer3_Init();
	KeyExti_Init();
	
//	OLED_ShowString(1,1,"Humi:  . %");
//	OLED_ShowString(3,1,"Temp:  . C");
//	OLED_ShowString(4,1,"Ligh:       lx");

	while (1)
	{
		if(DHT11_HumiAndTemp_Data(&DHT11_Data) == SUCCESS)
		{
			Serial_Printf("\r\n读取DHT11成功!\r\n湿度:%d.%d%%RH,温度:%d.%d℃ ",
							DHT11_Data.Humi_Int,DHT11_Data.Humi_Dec,DHT11_Data.Temp_Int,DHT11_Data.Temp_Dec);
		}
		else
		{
			Serial_Printf("\r\n读取DHT11失败!\r\n");
		}
		BH1750_read();
		Serial_Printf("\r\n读取BH1750成功!\r\n光照:%d.%dlx\r\n",(uint16_t)temp1,(uint16_t)temp2);
		
		if(Alarm_Free == 7)
		{
			if(DHT11_Data.Humi_Int<60 && DHT11_Data.Temp_Int<30 && temp1<100)
			{
				Alarm_Flag = 0;
			}
			else
			{
				Alarm_Flag = 1;
			}
		}
		else if(Alarm_Free < 7)
		{
			Alarm_Free++;
		}
		
		Delay_ms(1000);
		
		Serial_Printf("\r\nAlarm_Flag = %d\r\n", Alarm_Flag);
		Serial_Printf("\r\nAlarm_Free = %d\r\n", Alarm_Free);
	}
}
